import permission from "./permission"

class ImCamera {
	constructor() {
		this.stream = null;
	}
}

ImCamera.prototype.isEnable = function() {
	return !!navigator && !!navigator.mediaDevices && !!navigator.mediaDevices.getUserMedia;
}

ImCamera.prototype.openVideo =  function(isFacing) {
	if(this.stream){
		this.close();
	}
	return new Promise((resolve, reject) => {
		this.checkCameraPermission().then(() => {
			this.checkAudioPermission().then(() => {
				let facingMode = isFacing ? "user" : "environment";
				let constraints = {
					video: {
						with: window.screen.width,
						height: window.screen.height,
						facingMode: {
							exact: facingMode
						}
					},
					audio: {
						echoCancellation: true, //音频开启回音消除
						noiseSuppression: true // 开启降噪
					}
				}
				navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
					console.log("摄像头打开")
					this.stream = stream;
					resolve(stream);
				}).catch((e) => {
					console.log("摄像头未能正常打开")
					reject({
						code: 0,
						message: "摄像头未能正常打开"
					})
				})
			}).catch(() => {
				console.log("未能获取麦克风权限")
				reject({
					code: 0,
					message: "未能获取麦克风权限"
				})
			})
		}).catch(() => {
			console.log("未能获取摄像头访问权限")
			reject({
				code: 0,
				message: "未能获取摄像头访问权限"
			})
		})
	})
}

ImCamera.prototype.openAudio = function() {
	if(this.stream){
		this.close();
	}
	return new Promise((resolve, reject) => {
		this.checkAudioPermission().then(() => {
			let constraints = {
				video: false,
				audio: {
					echoCancellation: true, //音频开启回音消除
					noiseSuppression: true // 开启降噪
				}
			}
			navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
				this.stream = stream;
				resolve(stream);
			}).catch(() => {
				console.log("麦克风未能正常打开")
				reject({
					code: 0,
					message: "麦克风未能正常打开"
				})
			})
		}).catch(() => {
			console.log("未能获取麦克风权限")
			reject({
				code: 0,
				message: "未能获取麦克风权限"
			})
		})
	})
}

ImCamera.prototype.checkCameraPermission = function() {
	return new Promise((resolve, reject) => {
		// 只有APP才需要获取权限
		if (!window.plus) {
			resolve();
		}
		if (window.plus.os.name == "iOS") {
			// IOS只有在访问设备时才会弹出权限询问，这里判断了权限也没用
			//if (permission.judgeIosPermissionCamera()) {
				resolve();
			//} else {
				//reject();
			//}
		} else {
			permission.requestAndroidPermission('android.permission.CAMERA').then((res) => {
				if (res == 1) {
					resolve();
				} else {
					reject();
				}
			})
		}
	})
}

ImCamera.prototype.checkAudioPermission = function() {
	return new Promise((resolve, reject) => {
		// 只有APP才需要获取权限
		if (!window.plus) {
			resolve();
		}
		if (window.plus.os.name == "iOS") {
			// IOS只有在访问设备时才会弹出权限询问，这里判断了权限也没用
			//if (permission.judgeIosPermissionRecord()) {
				resolve();
			//} else {
			//	reject();
			//}
		} else {
			permission.requestAndroidPermission('android.permission.RECORD_AUDIO').then((res) => {
				if (res == 1) {
					resolve();
				} else {
					reject();
				}
			})
		}
	})
}

ImCamera.prototype.close = function() {
	// 停止流
	if (this.stream) {
		this.stream.getTracks().forEach((track) => {
			track.stop();
		});
		this.stream = null;
	}
}

export default ImCamera;